package com.copyctrl.campus.portal.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.campus.common.domain.User;
import com.copyctrl.campus.portal.dto.CommentDTO;
import com.copyctrl.campus.portal.dto.QuestionDTO;
import com.copyctrl.campus.portal.service.QuestionService;
import com.copyctrl.mapper.CampCommentMapper;
import com.copyctrl.mapper.CampQuestionMapper;
import com.copyctrl.model.CampComment;
import com.copyctrl.model.CampCommentExample;
import com.copyctrl.model.CampQuestion;
import com.copyctrl.model.CampQuestionExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    CampQuestionMapper mapper;

    @Autowired
    CampCommentMapper mapper1;

    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public int add(CampQuestion question) {
        return mapper.insert(question);
    }

    @Override
    public int delete(Integer question) {
        return mapper.deleteByPrimaryKey(question);
    }

    @Override
    public int update(CampQuestion question) {
        CampQuestionExample campQuestionExample = new CampQuestionExample();
        campQuestionExample.createCriteria().andIdEqualTo(question.getId());
        return mapper.updateByExampleSelective(question,campQuestionExample);
    }

    @Override
    public List<QuestionDTO> find() {
        CampQuestionExample example = new CampQuestionExample();
        example.createCriteria().andIdIsNotNull();
        return mapper.selectByExample(example)
                .stream().map(question -> {
                    String s = question.getUid().toString();
                    Object o = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, s);
                    String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + o);
                    QuestionDTO questionDTO = BeanUtil.copyProperties(question, QuestionDTO.class);
                    questionDTO.setUser(JSONUtil.toBean(s1, User.class));
                    return questionDTO;
                }).collect(Collectors.toList())
                ;
    }

    @Override
    public List<CommentDTO> findById(Integer id) {
        CampCommentExample example = new CampCommentExample();
        example.createCriteria().andTextIdEqualTo(id).andTypeEqualTo(2);
        List<CommentDTO> collect = mapper1.selectByExampleWithBLOBs(example)
                .stream()
                .map(comment -> {
                    CommentDTO commentDTO = BeanUtil.copyProperties(comment, CommentDTO.class);
                    String openid = comment.getUser();
                    String s = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
                    User user = JSONUtil.toBean(s, User.class);
                    commentDTO.setUserInfo(user);
                    String puser = comment.getPuser();
                    if (!StrUtil.isBlank(puser)) {
                        String s2 = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, puser).toString();
                        String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + s2);
                        User user1 = JSONUtil.toBean(s1, User.class);
                        commentDTO.setPUserInfo(user1);
                        commentDTO.setPuser(null);
                    }
                    commentDTO.setUser(null);
                    return commentDTO;
                }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int saveComment(CampComment comment) {
        return mapper1.insert(comment);
    }

    @Override
    public int deleteComment(Integer id) {
        return mapper1.deleteByPrimaryKey(id);
    }

    @Override
    public List<QuestionDTO> findByUid(Integer uid) {
        CampQuestionExample example = new CampQuestionExample();
        example.createCriteria().andUidEqualTo(uid);
        return mapper.selectByExample(example)
                .stream().map(question -> {
                    String s = question.getUid().toString();
                    Object o = redisTemplate.opsForHash().get(RedisConstant.ID_OPENID, s);
                    String s1 = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + o);
                    QuestionDTO questionDTO = BeanUtil.copyProperties(question, QuestionDTO.class);
                    questionDTO.setUser(JSONUtil.toBean(s1, User.class));
                    return questionDTO;
                }).collect(Collectors.toList());
    }
}
